package com.clk.portfoy;

import java.awt.Desktop;
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.ImageIcon;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import com.clk.portfoy.classlib.piyasa.Piyasa;
import com.clk.portfoy.classlib.portfoy.Portfoy;
import com.clk.portfoy.classlib.xml.XmlPortfoy;

public class PortfoyStatics {

	public static double YILLIKFAIZ_ORANI = 8;
	public static ImageIcon imgCLK = new ImageIcon(PortfoyStatics.class.getResource("images/CLK1.0.png"));
	public static ImageIcon imgClose = new ImageIcon(PortfoyStatics.class.getResource("images/close.png"));
	public static ImageIcon imgRefresh = new ImageIcon(PortfoyStatics.class.getResource("images/refresh.png"));
	public static ImageIcon imgUpArrow = new ImageIcon(PortfoyStatics.class.getResource("images/nav_up_green.png"));
	public static ImageIcon imgDownArrow = new ImageIcon(PortfoyStatics.class.getResource("images/nav_down_red.png"));
	public static ImageIcon imgStableBall = new ImageIcon(PortfoyStatics.class.getResource("images/nav_plain_blue.png"));
	public static Portfoy PORTFOY;
	public static Piyasa PIYASA;
	private static SimpleDateFormat formatter = new SimpleDateFormat("yyyy.MM.dd-HH:mm:ss");

	// ---------------------------------------------------------------------------------
	public static void xmlConfigW(Portfoy portfoy) {
		try {
			XmlPortfoy xp = new XmlPortfoy(portfoy);
			File file = new File("portfoy.xml");
			JAXBContext jaxbContext = JAXBContext.newInstance(XmlPortfoy.class);
			Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

			jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

			jaxbMarshaller.marshal(xp, file);
			jaxbMarshaller.marshal(xp, System.out);

		} catch (JAXBException e) {
			e.printStackTrace();
		}
	}

	// ---------------------------------------------------------------------------------
	public static Portfoy xmlConfigR() {
		XmlPortfoy xp = new XmlPortfoy();
		try {
			File file = new File("portfoy.xml");
			if (file.exists()) {
				JAXBContext jaxbContext = JAXBContext.newInstance(XmlPortfoy.class);

				Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
				xp = (XmlPortfoy) jaxbUnmarshaller.unmarshal(file);
				System.out.println(xp);
			}
		} catch (JAXBException e) {
			e.printStackTrace();
		}
		return xp.getPortfoyEqual();
	}

	// ---------------------------------------------------------------------------------
	public static String getDateStr(Date date) {
		if (date != null) {
			return formatter.format(date);
		}
		return "";
	}

	// ---------------------------------------------------------------------------------
	public static String getTimeStr(Date date) {
		SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
		if (date != null) {
			return formatter.format(date);
		}
		return "";
	}

	// ---------------------------------------------------------------------------------
	public static String getDoubleStr(double value) {
		DecimalFormat twoDec = new DecimalFormat("0.00");
		return twoDec.format(value);
	}

	// ---------------------------------------------------------------------------------
	public static Date getDateEqual(String date) throws ParseException {
		return formatter.parse(date);
	}

	// ---------------------------------------------------------------------------------
	public static double getDoubleEqual(String value) {
		return Double.parseDouble(value);
	}

	// ---------------------------------------------------------------------------------
	public static void openURI(URI uri) {
		if (Desktop.isDesktopSupported()) {
			try {
				Desktop.getDesktop().browse(uri);
			} catch (IOException e) { /* TODO: error handling */
			}
		} else { /* TODO: error handling */

		}
	}
	// ---------------------------------------------------------------------------------
}
